﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    public static partial class ExtensionHelper
    {
        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList2<T>(this IQueryable<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex - 1) * pageSize;
            var totalItemCount = allItems.Count();
            if (isAlwaysReturnLastPageData)
            {
                while (totalItemCount <= itemIndex && pageIndex > 1)
                {
                    itemIndex = (--pageIndex - 1) * pageSize;
                }
            }
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList2<T>(this IList<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex - 1) * pageSize;
            var totalItemCount = allItems.Count();
            if (isAlwaysReturnLastPageData)
            {
                while (totalItemCount <= itemIndex && pageIndex > 1)
                {
                    itemIndex = (--pageIndex - 1) * pageSize;
                }
            }
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedListToJs<T> ToPagedList3<T>(this IQueryable<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            var page = ToPagedList2(allItems, pageIndex, pageSize, isAlwaysReturnLastPageData);
            return new PagedListToJs<T>() { CurrentPageIndex = page.CurrentPageIndex, Data = page, EndItemIndex = page.EndItemIndex, PageSize = page.PageSize, StartItemIndex = page.StartItemIndex, TotalItemCount = page.TotalItemCount, TotalPageCount = page.TotalPageCount };
        }

        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedListToJs<T> ToPagedList3<T>(this IList<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            var page = ToPagedList2(allItems, pageIndex, pageSize, isAlwaysReturnLastPageData);
            return new PagedListToJs<T>() { CurrentPageIndex = page.CurrentPageIndex, Data = page, EndItemIndex = page.EndItemIndex, PageSize = page.PageSize, StartItemIndex = page.StartItemIndex, TotalItemCount = page.TotalItemCount, TotalPageCount = page.TotalPageCount };
        }

        /// <summary>
        /// 分页扩展(返回支持layui格式的数据)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">查询页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedListToLayui<T> ToPagedListToLayui<T>(this IQueryable<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            var page = ToPagedList2(allItems, pageIndex, pageSize, isAlwaysReturnLastPageData);
            return new PagedListToLayui<T>() { page = page.CurrentPageIndex, data = page, limit = page.PageSize, count = page.TotalItemCount, totalpage = page.TotalPageCount };
        }

        /// <summary>
        /// 分页扩展(返回支持layui格式的数据)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems"></param>
        /// <param name="pageIndex">查询页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="isAlwaysReturnLastPageData">是否始终返回最后一页数据(当pageindex大于总页数时候)</param>
        /// <returns></returns>
        public static PagedListToLayui<T> ToPagedListToLayui<T>(this IList<T> allItems, int pageIndex, int pageSize, bool isAlwaysReturnLastPageData = false)
        {
            var page = ToPagedList2(allItems, pageIndex, pageSize, isAlwaysReturnLastPageData);
            return new PagedListToLayui<T>() { page = page.CurrentPageIndex, data = page, limit = page.PageSize, count = page.TotalItemCount, totalpage = page.TotalPageCount };
        }
    }
    /// <summary>
    /// 数据集合(layui格式的数据)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedListToLayui<T>
    {
        public List<T> data { get; set; }
        /// <summary>
        /// 总数据条数
        /// </summary>
        public int count { get; set; }

        /// <summary>
        /// 状态码(默认为0)
        /// </summary>
        public int code { get; set; } = 0;

        /// <summary>
        /// 消息
        /// </summary>
        public string msg { get; set; } = "";
        /// <summary>
        /// 当前索引页
        /// </summary>
        public int page { get; set; }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int limit { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int totalpage { get; set; }
    }

    public class PagedListToJs<T>
    {
        public PagedList<T> Data { get; set; }
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int CurrentPageIndex { get; set; }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总数据条数
        /// </summary>
        public int TotalItemCount { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get; set; }
        /// <summary>
        /// 开始索引
        /// </summary>
        public int StartItemIndex { get; set; }
        /// <summary>
        /// 结束索引
        /// </summary>
        public int EndItemIndex { get; set; }
    }

    public class PagedList<T> : List<T>
    {
        public PagedList(IQueryable<T> currentPageItems, int pageIndex, int pageSize, int totalItemCount)
        {
            AddRange(currentPageItems);
            TotalItemCount = totalItemCount;
            CurrentPageIndex = pageIndex;
            PageSize = pageSize;
        }

        public PagedList(IEnumerable<T> currentPageItems, int pageIndex, int pageSize, int totalItemCount)
        {
            AddRange(currentPageItems);
            TotalItemCount = totalItemCount;
            CurrentPageIndex = pageIndex;
            PageSize = pageSize;
        }

        /// <summary>
        /// 当前页索引
        /// </summary>
        public int CurrentPageIndex { get; set; }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总数据条数
        /// </summary>
        public int TotalItemCount { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get { return (int)Math.Ceiling(TotalItemCount / (double)PageSize); } }
        /// <summary>
        /// 开始索引
        /// </summary>
        public int StartItemIndex { get { return (CurrentPageIndex - 1) * PageSize + 1; } }
        /// <summary>
        /// 结束索引
        /// </summary>
        public int EndItemIndex { get { return TotalItemCount > CurrentPageIndex * PageSize ? CurrentPageIndex * PageSize : TotalItemCount; } }
    }




}
